﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANZ_AIM_GUI.Utils;
using ANZ_AIM_BLL;
using ANZ_AIM_FRAMEWORK;
using ANZ_AIM_BLL.Model;

namespace ANZ_AIM_GUI
{
    public partial class frmCandidateOffline : Form
    {
        public int OpMode { get; set; }
        public CandidateOffline co { get; set; }

        public frmCandidateOffline()
        {
            InitializeComponent();
        }

        public frmCandidateOffline(int mode, CandidateOffline co)
            : this()
        {
            try
            {
                this.OpMode = mode;
                this.co = co;

                InitData();
                switch (OpMode)
                {
                    case MiscHelper.FORM_OPMODE_ADD_NEW:
                        this.Text = "ADD CANDIDATE OFFLINE";
                        break;
                    case MiscHelper.FORM_OPMODE_EDIT:
                        this.Text = "EDIT CANDIDATE OFFLINE";
                        btnAdd.Text = "UPDATE";

                        //unpack object
                        UnPackObjectData_Candidate();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        #region Action

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                //if (!this.validateForm())
                //    return;

                this.PackObjectData_CandidateOffline();
                switch (OpMode)
                {
                    case MiscHelper.FORM_OPMODE_ADD_NEW:
                        MessageBox.Show("New Candidate added.", Constant.SYS_INFORMATION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                        break;
                    case MiscHelper.FORM_OPMODE_EDIT:
                        MessageBox.Show("Candidate saved.", Constant.SYS_INFORMATION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            //group personalinfo
            txtPersonalCode.Text = "";
            cbChannel.SelectedIndex = 0;
            txtFullNameE.Text = "";
            txtFullNameV.Text = "";
            txtCellPhone.Text = "";
            dtDOB.IsEmpty = true;
            txtCollege.Text = "";
            txtQualification.Text = "";
            txtInterviewer.Text = "";
            txtIDNumber.Text = "";
            dtIssuedDate.IsEmpty = true;
            cbIssuedPlace.SelectedIndex = 0;
            txtPermanentAddress.Text = "";
            dtInterviewDate.IsEmpty = true;
            cbTrainingCallStatus.SelectedIndex = 0;
            txtTraningCallStatusRemark.Text = "";

            //group pre-employeecheck
            dtCICSactionDeliveryDate.IsEmpty = true;
            txtCICSanctionDN.Text = "";
            cbCICResult.SelectedIndex = 0;
            cbSanctionResult.SelectedIndex = 0;
            cbPECCheckResult.SelectedIndex = 0;
            txtPECRemark.Text = "";

            //group on-boarding training
            txtTrainingBatch.Text = "";
            dtTrainingDate.IsEmpty = true;
            txtTrainerName.Text = "";
            cbTrainingAttendance.SelectedIndex = 0;
            txtTraingingAttendanceRemark.Text = "";
            cbReTest.SelectedIndex = 0;
            txtRetestRequirement.Text = "";
            txtTrainingResult.Text = "";

            //group on-boarding documentation
            cbOBEligible.SelectedIndex = 0;
            cbDocumentCompletion.SelectedIndex = 0;
            txtDocumentIncompletionRemark.Text = "";
            dtPCReceipt.IsEmpty = true;
            dtPCReport.IsEmpty = true;

            //group onboarding-approval
            cbHiringManagerName.SelectedIndex = 0;
            dtHMAckDate.IsEmpty = true;
            txtDelegateApproveName.Text = "";
            dtApprovingDate.IsEmpty = true;
            cbOnBoarding.SelectedIndex = 0;
            dtOnboardingRemark.IsEmpty = true;
            dtCICSanctionRequest.IsEmpty = true;
            cbTrainingResultCC.SelectedIndex = 0;
            txtCheckChannel.Text = "";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region InitData
        private void InitData()
        {
            loadCity(cbIssuedPlace);
            loadStatus(cbTrainingCallStatus);
            loadStatus(cbTrainingAttendance);
            loadStatus(cbPECCheckResult);
            loadStatus(cbDocumentCompletion);
            loadStatus(cbOBEligible);
            loadStatus(cbOnBoarding);
            loadHiringManagerName();
            loadSanctionResult();
            loadCICResult();
            loadRetest();
            loadTrainingResulCC();
            loadChannel(cbChannel);
        }

        private void loadChannel(ComboBox cbData)
        {
            cbData.Items.AddRange(new string[] { "DS-HCM 1",
                                                    "DS-HCM 2",
                                                    "DS-HCM 3",
                                                    "DS-HN 1",
                                                    "DS-HN 2",
                                                    "TS In-house 1",
                                                    "TS In-house 2",
                                                    "TS In-house 3",
                                                    "TS In-house 4",
                                                    "TS In-house HN 1",
                                                    "TS In-house HN 2",
                                                    "TS In-house HN 3",
                                                    "DS-BD",
                                                    "TS Portfolio",
                                                    "Field Sales"
                                                   });

            cbData.SelectedIndex = 0;
        }

        private void loadCity(ComboBox cbData)
        {
            cbData.Items.AddRange(new string[] {    "An Giang",
                                                    "Bà Rịa - Vũng Tàu",
                                                    "Bắc Giang",
                                                    "Bắc Kạn",
                                                    "Bạc Liêu",
                                                    "Bắc Ninh",
                                                    "Bến Tre",
                                                    "Bình Định",
                                                    "Bình Dương",
                                                    "Bình Phước",
                                                    "Bình Thuận",
                                                    "Cà Mau",
                                                    "Cần Thơ",
                                                    "Cao Bằng",
                                                    "Đà Nẵng",
                                                    "Đắk Lắk",
                                                    "Đắk Nông",
                                                    "Điện Biên",
                                                    "Đồng Nai",
                                                    "Đồng Tháp",
                                                    "Gia Lai",
                                                    "Hà Giang",
                                                    "Hà Nam",
                                                    "Hà Nội",
                                                    "Hà Tĩnh",
                                                    "Hải Dương",
                                                    "Hải Phòng",
                                                    "Hậu Giang",
                                                    "Hòa Bình",
                                                    "Hưng Yên",
                                                    "Khánh Hòa",
                                                    "Kiên Giang",
                                                    "Kon Tum",
                                                    "Lai Châu",
                                                    "Lâm Đồng",
                                                    "Lạng Sơn",
                                                    "Lào Cai",
                                                    "Long An",
                                                    "Nam Định",
                                                    "Nghệ An",
                                                    "Ninh Bình",
                                                    "Phú Yên",
                                                    "Quảng Bìn",
                                                    "Quảng Nam",
                                                    "Quảng Ngãi",
                                                    "Quảng Ninh",
                                                    "Quảng Trị",
                                                    "Sóc Trăng",
                                                    "Sơn La",
                                                    "Tây Ninh",
                                                    "Thái Bình",
                                                    "Thái Nguyên",
                                                    "Thừa Thiên Huế",
                                                    "Tiền Giang",
                                                    "TP HCM",
                                                    "Trà Vinh",
                                                    "Tuyên Quang",
                                                    "Vĩnh Long",
                                                    "Vũng Tàu",
                                                    "Vĩnh Phúc",
                                                    "Yên Bái"
                                                   });

            cbIssuedPlace.SelectedIndex = 0;
        }

        private void loadStatus(ComboBox cbData)
        {
            cbData.Items.AddRange(new string[] { "1.Yes",
                                                    "2.No"
                                                   });
            cbData.SelectedIndex = 0;
        }

        private void loadHiringManagerName()
        {
            cbHiringManagerName.Items.AddRange(new string[] { "Văn Thiên Quốc Hữu",
                                                    "Trang Thanh Hoàng",
                                                    "Trần Tuấn Kiệt",
                                                    "Nguyễn Thi Thu",
                                                    "Nguyễn Vĩnh Tùng",
                                                    "Nguyễn Thiị Thúy Diễm",
                                                    "Trần Thị Ánh Phương",
                                                    "Phan Thị Thu Hiền",
                                                    "Phan Thị Thu Hiền",
                                                    "Nguyễn Thu Trà",
                                                    "Nguyễn Thu Trà",
                                                    "Nguyễn Hồng Thái",
                                                    "Trang Thanh Hoàng",
                                                    "Nguyễn Thị Thúy Diễm",
                                                    "Trần Thị Ánh Phương"
                                                   });
            cbHiringManagerName.SelectedIndex = 0;
        }

        private void loadSanctionResult()
        {
            cbSanctionResult.Items.AddRange(new string[] { "Positive"
                                                   });
            cbSanctionResult.SelectedIndex = 0;
        }

        private void loadCICResult()
        {
            cbCICResult.Items.AddRange(new string[] { "No Loan"
                                                   });
            cbCICResult.SelectedIndex = 0;
        }

        private void loadRetest()
        {
            cbReTest.Items.AddRange(new string[] { "2.1. ANZ Instroduction",
                                                    "2.2. Compliance",
                                                    "2.3 Product",
                                                    "2.4. ANZ Introduction & Compliance",
                                                    "2.5. ANZ Introduction & Product",
                                                    "2.6. Compliance & Product",
                                                    "2.7. All"
                                                   });

            cbReTest.SelectedIndex = 0;
        }

        private void loadTrainingResulCC()
        {
            cbTrainingResultCC.Items.AddRange(new string[] { "Pass" });
            cbTrainingResultCC.SelectedIndex = 0;
        }

        #endregion

        private bool validateForm()
        {
            try
            {
                if (string.IsNullOrEmpty(txtPersonalCode.Text))
                {
                    MessageBox.Show("Personal Code cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPersonalCode.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtFullNameE.Text))
                {
                    MessageBox.Show("Full Name E cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtFullNameE.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtFullNameV.Text))
                {
                    MessageBox.Show("Full Name V cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtFullNameV.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtCellPhone.Text))
                {
                    MessageBox.Show("Cell Phone cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtCellPhone.Focus();
                    return false;
                }
                if (dtDOB.IsEmpty)
                {
                    MessageBox.Show("Date Of Birth cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dtDOB.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtQualification.Text))
                {
                    MessageBox.Show("Qualification cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtQualification.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtCollege.Text))
                {
                    MessageBox.Show("College cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtCollege.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtIDNumber.Text))
                {
                    MessageBox.Show("ID Number cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtIDNumber.Focus();
                    return false;
                }
                if (dtIssuedDate.IsEmpty)
                {
                    MessageBox.Show("Issued Date cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dtIssuedDate.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtPermanentAddress.Text))
                {
                    MessageBox.Show("Permanent Address cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPermanentAddress.Focus();
                    return false;
                }

                if (string.IsNullOrEmpty(txtInterviewer.Text))
                {
                    MessageBox.Show("Interviewer cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtInterviewer.Focus();
                    return false;
                }
                if (dtInterviewDate.IsEmpty)
                {
                    MessageBox.Show("Interview Date cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dtInterviewDate.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtTraningCallStatusRemark.Text))
                {
                    MessageBox.Show("Traning Call Status Remark cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTraningCallStatusRemark.Focus();
                    return false;
                }

                if (dtCICSactionDeliveryDate.IsEmpty)
                {
                    MessageBox.Show("CIC Saction Delivery Date cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dtCICSactionDeliveryDate.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtCICSanctionDN.Text))
                {
                    MessageBox.Show("CIC Sanaction Deliverer Name cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtCICSanctionDN.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtPECRemark.Text))
                {
                    MessageBox.Show("PEC Remark  cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPECRemark.Focus();
                    return false;
                }

                if (string.IsNullOrEmpty(txtTrainingBatch.Text))
                {
                    MessageBox.Show("Training Batch cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTrainingBatch.Focus();
                    return false;
                }
                if (dtTrainingDate.IsEmpty)
                {
                    MessageBox.Show("Training Date cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dtTrainingDate.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtTrainerName.Text))
                {
                    MessageBox.Show("Trainer Name cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTrainerName.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtTraingingAttendanceRemark.Text))
                {
                    MessageBox.Show("Training Attendance Remark cannot be empty", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTraingingAttendanceRemark.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtRetestRequirement.Text))
                {
                    MessageBox.Show("Re-test Requirement cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtRetestRequirement.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtTrainingResult.Text))
                {
                    MessageBox.Show("Training Result cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTrainingResult.Focus();
                    return false;
                }
                return true;
            }
            catch (Exception ex) { throw; }

        }

        private void UnPackObjectData_Candidate()
        {
            txtPersonalCode.Text = co.PersonalCode;
            cbChannel.Text = co.Channel;
            txtFullNameE.Text = co.FullName_E;
            txtFullNameV.Text = co.FullName_V;
            txtCellPhone.Text = co.CellPhone;
            dtDOB.Value = Convert.ToDateTime(co.DateOfBirth != null ? co.DateOfBirth : (DateTime?)null);
            txtQualification.Text = co.Qualification;
            txtCollege.Text = co.College;
            txtIDNumber.Text = co.IDNumber;
            dtIssuedDate.Value = Convert.ToDateTime(co.IssuedDate != null ? co.IssuedDate : (DateTime?)null);
            cbIssuedPlace.Text = co.IssuedPlace;
            txtPermanentAddress.Text = co.PermanentAddress;
            txtInterviewer.Text = co.Interviewer;
            dtInterviewDate.Value = Convert.ToDateTime(co.InterviewDate != null ? co.InterviewDate : (DateTime?)null);
            cbTrainingCallStatus.Text = co.TrainingCallStatus;
            txtTraningCallStatusRemark.Text = co.TrainingCallStatusRemarks;

            dtCICSactionDeliveryDate.Value = Convert.ToDateTime(co.CICSanctionDeliveryDate != null ? co.CICSanctionDeliveryDate : (DateTime?)null);
            txtCICSanctionDN.Text = co.CICSanctionDelivererName;
            cbCICResult.Text = co.CICResult;
            cbSanctionResult.Text = co.SanctionResult;
            cbPECCheckResult.Text = co.PECCheckResult;
            txtPECRemark.Text = co.PECRemarks;

            txtTrainingBatch.Text = co.TrainingBatch;
            dtTrainingDate.Value = Convert.ToDateTime(co.TrainingDate != null ? co.TrainingDate : (DateTime?)null);
            txtTrainerName.Text = co.TrainerName;
            cbTrainingAttendance.Text = co.TrainingAttendance;
            txtTraingingAttendanceRemark.Text = co.TrainingAttendanceRemarks;
            cbReTest.Text = co.ReTest;
            txtRetestRequirement.Text = co.ReTestRequirement;
            txtTrainingResult.Text = co.TrainingResult;

            cbOBEligible.Text = co.OBEligible;
            cbDocumentCompletion.Text = co.DocumentCompletion;
            txtDocumentIncompletionRemark.Text = co.DocumentIncompletionRemarks;
            dtPCReceipt.Value = Convert.ToDateTime(co.PCReceiptSubmissionDate != null ? co.PCReceiptSubmissionDate : (DateTime?)null);
            dtPCReport.Value = Convert.ToDateTime(co.PCReportSubmissionDate != null ? co.PCReportSubmissionDate : (DateTime?)null);

            cbHiringManagerName.Text = co.HiringManagerName;
            dtHMAckDate.Value = Convert.ToDateTime(co.HiringManagerAcknowledgeDate != null ? co.HiringManagerAcknowledgeDate : (DateTime?)null);
            txtDelegateApproveName.Text = co.DelegateApproverName;
            dtApprovingDate.Value = Convert.ToDateTime(co.ApprovingDate != null ? co.ApprovingDate : (DateTime?)null);
            cbOnBoarding.Text = co.OnBoarding;
            dtOnboardingRemark.Value = Convert.ToDateTime(co.OnBoardingRemarks != null ? co.OnBoardingRemarks : (DateTime?)null);
            dtCICSanctionRequest.Value = Convert.ToDateTime(co.CICSanctionRequestDate != null ? co.CICSanctionRequestDate : (DateTime?)null);
            cbTrainingResultCC.Text = co.TrainingResultCC;
            txtCheckChannel.Text = co.CheckChannel;
        }

        private void PackObjectData_CandidateOffline()
        {
            co.PersonalCode = txtPersonalCode.Text.Trim();
            co.Channel = cbChannel.Text;
            co.FullName_E = txtFullNameE.Text.Trim();
            co.FullName_V = txtFullNameV.Text.Trim();
            co.CellPhone = txtCellPhone.Text.Trim();
            co.DateOfBirth = MiscHelper.DateTimeToShort(dtDOB.Value);
            co.Qualification = txtQualification.Text;
            co.College = txtCollege.Text;
            co.IDNumber = txtIDNumber.Text.Trim();
            co.IssuedDate = MiscHelper.DateTimeToShort(dtIssuedDate.Value);
            co.IssuedPlace = cbIssuedPlace.Text;
            co.PermanentAddress = txtPermanentAddress.Text.Trim();
            co.Interviewer = txtInterviewer.Text.Trim();
            co.InterviewDate = MiscHelper.DateTimeToShort(dtInterviewDate.Value);
            co.TrainingCallStatus = cbTrainingCallStatus.Text;
            co.TrainingCallStatusRemarks = txtTraningCallStatusRemark.Text.Trim();

            co.CICSanctionDeliveryDate = MiscHelper.DateTimeToShort(dtCICSactionDeliveryDate.Value);
            co.CICSanctionDelivererName = txtCICSanctionDN.Text.Trim();
            co.CICResult = cbCICResult.Text;
            co.SanctionResult = cbSanctionResult.Text;
            co.PECCheckResult = cbPECCheckResult.Text;
            co.PECRemarks = txtPECRemark.Text.Trim();

            co.TrainingBatch = txtTrainingBatch.Text.Trim();
            co.TrainingDate = MiscHelper.DateTimeToShort(dtTrainingDate.Value);
            co.TrainerName = txtTrainerName.Text.Trim();
            co.TrainingAttendance = cbTrainingAttendance.Text;
            co.TrainingAttendanceRemarks = txtTraingingAttendanceRemark.Text;
            co.ReTest = cbReTest.Text;
            co.ReTestRequirement = txtRetestRequirement.Text.Trim();
            co.TrainingResult = txtTrainingResult.Text.Trim();

            co.OBEligible = cbOBEligible.Text;
            co.DocumentCompletion = cbDocumentCompletion.Text;
            co.DocumentIncompletionRemarks = txtDocumentIncompletionRemark.Text;
            co.PCReceiptSubmissionDate = MiscHelper.DateTimeToShort(dtPCReceipt.Value);
            co.PCReportSubmissionDate = MiscHelper.DateTimeToShort(dtPCReport.Value);

            co.HiringManagerName = cbHiringManagerName.Text;
            co.HiringManagerAcknowledgeDate = MiscHelper.DateTimeToShort(dtHMAckDate.Value);
            co.DelegateApproverName = txtDelegateApproveName.Text.Trim();
            co.ApprovingDate = MiscHelper.DateTimeToShort(dtApprovingDate.Value);
            co.OnBoarding = cbOnBoarding.Text;
            co.OnBoardingRemarks = MiscHelper.DateTimeToShort(dtOnboardingRemark.Value);
            co.CICSanctionRequestDate = MiscHelper.DateTimeToShort(dtCICSanctionRequest.Value);
            co.TrainingResultCC = cbTrainingResultCC.Text;
            co.CheckChannel = txtCheckChannel.Text;
        }

        private void txtCellPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (!char.IsDigit(e.KeyChar)) && (!char.IsControl(e.KeyChar));
        }

        private void txtIDNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (!char.IsDigit(e.KeyChar)) && (!char.IsControl(e.KeyChar));
        }


    }
}
